
package com.mimc;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.mimc.comon.UserManager;
import com.xiaomi.mimc.MIMCGroupMessage;
import com.xiaomi.mimc.MIMCMessage;
import com.xiaomi.mimc.MIMCOnlineMessageAck;
import com.xiaomi.mimc.MIMCServerAck;
import com.xiaomi.mimc.common.MIMCConstant;
import java.util.List;

public class MIMCModule extends ReactContextBaseJavaModule implements UserManager.OnHandleMIMCMsgListener {

  private final ReactApplicationContext reactContext;
  private DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
  private static final String TAG = "MIMCModule";


  public MIMCModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
    UserManager.getInstance().setContext(reactContext.getApplicationContext());
    UserManager.getInstance().setHandleMIMCMsgListener(this);
  }

  @Override
  public String getName() {
    return "MIMCModule";
  }


  /**
   * 初始化MiMC的token
   */
  @ReactMethod
  public void initToken(String token){
    UserManager.getInstance().setToken(token);
  }



  @ReactMethod
  public void login(String appId, String appAccount, String resolverUrl, String ucUrl) {
    UserManager.getInstance().newMIMCUser(Long.parseLong(appId), appAccount, resolverUrl, ucUrl);
  }

  @ReactMethod
  public void logout() {
    UserManager.getInstance().logout();
  }

  /**
   * 发送单聊消息
   * @param params
   */
  @ReactMethod
  public void sendMessage(ReadableMap params){
    String toAccount = params.getString("toAccount");
    String payload = params.getString("payload");
    String bizType = params.getString("bizType");
    UserManager.getInstance().sendMessage(toAccount, payload, bizType);
  }

  /**
   * 发送群聊消息
   * @param params
   */
  @ReactMethod
  public void sendGroupMessage(ReadableMap params){
    long topicId = Long.parseLong(params.getString("topicId"));
    String payload = params.getString("payload");
    String bizType = params.getString("bizType");
    UserManager.getInstance().sendGroupMessage(topicId, payload, bizType);
  }

  /**
   * 发送在线消息
   * @param params
   */
  @ReactMethod
  public void sendOnlineMessage(ReadableMap params){
    String toAccount = params.getString("toAccount");
    String payload = params.getString("payload");
    String bizType = params.getString("bizType");
    UserManager.getInstance().sendOnlineMessage(toAccount, payload, bizType);
  }

  /**
   * 发送无限群消息
   * @param params
   */
  @ReactMethod
  public void sendUnlimitedGroupMessage(ReadableMap params){
    long topicId = Long.parseLong(params.getString("topicId"));
    String payload = params.getString("payload");
    String bizType = params.getString("bizType");
    UserManager.getInstance().sendUnlimitedGroupMessage(topicId, payload, bizType);
  }



  @Override
  public void onHandleMessage(List<MIMCMessage> messageList) {
    WritableArray array = Arguments.createArray();
    for (MIMCMessage msg : messageList) {
      WritableMap map = Arguments.createMap();
      map.putString("packetId", msg.getPacketId());
      map.putString("sequence", msg.getSequence() + "");
      map.putString("timestamp", msg.getTimestamp() + "");
      map.putString("fromAccount", msg.getFromAccount());
      map.putString("fromResource", msg.getFromResource());
      map.putString("toAccount", msg.getToAccount());
      map.putString("toResource", msg.getToResource());
      map.putString("bizType", msg.getBizType());
      map.putString("convIndex", msg.getConvIndex() + "");
      map.putString("payload", new String(msg.getPayload()));
      array.pushMap(map);
    }
    sendEvent("onHandleMIMCMessage", array);
  }

  @Override
  public void onHandleGroupMessage(List<MIMCGroupMessage> groupMessageList) {
    WritableArray array = Arguments.createArray();
    for (MIMCGroupMessage msg : groupMessageList) {
      WritableMap map = Arguments.createMap();
      map.putString("packetId", msg.getPacketId());
      map.putString("sequence", msg.getSequence() + "");
      map.putString("timestamp", msg.getTimestamp() + "");
      map.putString("fromAccount", msg.getFromAccount());
      map.putString("fromResource", msg.getFromResource());
      map.putString("bizType", msg.getBizType());
      map.putString("topicId", msg.getTopicId() + "");
      map.putString("payload", new String(msg.getPayload()));
      array.pushMap(map);
    }
    sendEvent("onHandleMIMCGroupMessage", array);
  }

  @Override
  public void onHandleOnlineMessage(List<MIMCMessage> onlineMessageList) {
    WritableArray array = Arguments.createArray();
    for (MIMCMessage msg : onlineMessageList) {
      WritableMap map = Arguments.createMap();
      map.putString("packetId", msg.getPacketId());
      map.putString("sequence", msg.getSequence() + "");
      map.putString("timestamp", msg.getTimestamp() + "");
      map.putString("fromAccount", msg.getFromAccount());
      map.putString("fromResource", msg.getFromResource());
      map.putString("toAccount", msg.getToAccount());
      map.putString("toResource", msg.getToResource());
      map.putString("bizType", msg.getBizType());
      map.putString("convIndex", msg.getConvIndex() + "");
      map.putString("payload", new String(msg.getPayload()));
      array.pushMap(map);
    }
    sendEvent("onHandleMIMCOnlineMessage", array);
  }

  @Override
  public void onHandleUnlimitedGroupMessage(List<MIMCGroupMessage> unlimitedGroupMessageList) {
    WritableArray array = Arguments.createArray();
    for (MIMCGroupMessage msg : unlimitedGroupMessageList) {
      WritableMap map = Arguments.createMap();
      map.putString("packetId", msg.getPacketId());
      map.putString("sequence", msg.getSequence() + "");
      map.putString("timestamp", msg.getTimestamp() + "");
      map.putString("fromAccount", msg.getFromAccount());
      map.putString("fromResource", msg.getFromResource());
      map.putString("bizType", msg.getBizType());
      map.putString("topicId", msg.getTopicId() + "");
      map.putString("payload", new String(msg.getPayload()));
      array.pushMap(map);
    }
    sendEvent("onHandleMIMCUnlimitedGroupMessage", array);
  }

  @Override
  public void onHandleServerAck(MIMCServerAck serverAck) {
    WritableMap map = Arguments.createMap();
    map.putString("packetId", serverAck.getPacketId());
    map.putString("sequence", serverAck.getSequence() + "");
    map.putString("timestamp", serverAck.getTimestamp() + "");
    map.putInt("code", serverAck.getCode());
    map.putString("desc", serverAck.getDesc());
    map.putString("convIndex", serverAck.getConvIndex() + "");
    sendEvent("onHandleMIMCServerAck", map);
  }

  @Override
  public void onHandleOnlineMessageAck(MIMCOnlineMessageAck onlineMessageAck) {
    WritableMap map = Arguments.createMap();
    map.putString("packetId", onlineMessageAck.getPacketId());
    map.putInt("code", onlineMessageAck.getCode());
    map.putString("desc", onlineMessageAck.getDesc());
    sendEvent("onHandleMIMCOnlineMessageAck", map);
  }

  @Override
  public void onHandleSendMessageTimeout(MIMCMessage msg) {
    WritableMap map = Arguments.createMap();
    map.putString("packetId", msg.getPacketId());
    map.putString("sequence", msg.getSequence() + "");
    map.putString("timestamp", msg.getTimestamp() + "");
    map.putString("fromAccount", msg.getFromAccount());
    map.putString("fromResource", msg.getFromResource());
    map.putString("toAccount", msg.getToAccount());
    map.putString("toResource", msg.getToResource());
    map.putString("bizType", msg.getBizType());
    map.putString("convIndex", msg.getConvIndex() + "");
    map.putString("payload", new String(msg.getPayload()));
    sendEvent("onHandleMIMCSendMessageTimeout", map);
  }

  @Override
  public void onHandleSendGroupMessageTimeout(MIMCGroupMessage msg) {
    WritableMap map = Arguments.createMap();
    map.putString("packetId", msg.getPacketId());
    map.putString("sequence", msg.getSequence() + "");
    map.putString("timestamp", msg.getTimestamp() + "");
    map.putString("fromAccount", msg.getFromAccount());
    map.putString("fromResource", msg.getFromResource());
    map.putString("bizType", msg.getBizType());
    map.putString("topicId", msg.getTopicId() + "");
    map.putString("payload", new String(msg.getPayload()));
    sendEvent("onHandleMIMCSendGroupMessageTimeout", map);
  }

  @Override
  public void onHandleSendUnlimitedGroupMessageTimeout(MIMCGroupMessage msg) {
    WritableMap map = Arguments.createMap();
    map.putString("packetId", msg.getPacketId());
    map.putString("sequence", msg.getSequence() + "");
    map.putString("timestamp", msg.getTimestamp() + "");
    map.putString("fromAccount", msg.getFromAccount());
    map.putString("fromResource", msg.getFromResource());
    map.putString("bizType", msg.getBizType());
    map.putString("topicId", msg.getTopicId() + "");
    map.putString("payload", new String(msg.getPayload()));
    sendEvent("onHandleMIMCSendUnlimitedGroupMessageTimeout", map);
  }

  @Override
  public void onPullNotification() {

  }

  @Override
  public void onHandleStatusChanged(MIMCConstant.OnlineStatus status, String type, String reason, String desc) {
    WritableMap map = Arguments.createMap();
    if(status == MIMCConstant.OnlineStatus.ONLINE){
      map.putInt("status", 1);
    } else {
      map.putInt("status", 0);
    }
    map.putString("type", type);
    map.putString("reason", reason);
    map.putString("desc", desc);
    sendEvent("onHandleMIMCStatusChanged", map);
  }

  @Override
  public void onHandleCreateUnlimitedGroup(long topicId, int code, String desc) {
    WritableMap map = Arguments.createMap();
    map.putString("topicId", topicId + "");
    map.putInt("code", code);
    map.putString("desc", desc);
    sendEvent("onHandleMIMCCreateUnlimitedGroup", map);
  }

  @Override
  public void onHandleJoinUnlimitedGroup(long topicId, int code, String desc) {
    WritableMap map = Arguments.createMap();
    map.putString("topicId", topicId + "");
    map.putInt("code", code);
    map.putString("desc", desc);
    sendEvent("onHandleMIMCJoinUnlimitedGroup", map);
  }

  @Override
  public void onHandleQuitUnlimitedGroup(long topicId, int code, String desc) {
    WritableMap map = Arguments.createMap();
    map.putString("topicId", topicId + "");
    map.putInt("code", code);
    map.putString("desc", desc);
    sendEvent("onHandleMIMCQuitUnlimitedGroup", map);
  }

  @Override
  public void onHandleDismissUnlimitedGroup(long topicId, int code, String desc) {
    WritableMap map = Arguments.createMap();
    map.putString("topicId", topicId + "");
    map.putInt("code", code);
    map.putString("desc", desc);
    sendEvent("onHandleMIMCDismissUnlimitedGroup", map);
  }


  @Override
  public void onHandleDismissUnlimitedGroup(long topicId) {
    WritableMap map = Arguments.createMap();
    map.putString("topicId", topicId + "");
    map.putInt("code", 500);
    map.putString("desc", "onHandleDismissUnlimitedGroup success");
    sendEvent("onHandleMIMCDismissUnlimitedGroup", map);
  }


  @Override
  public void onHandleCreateGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQueryGroupInfo(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQueryGroupsOfAccount(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleJoinGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQuitGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleKickGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleUpdateGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleDismissGroup(String json, boolean isSuccess) {

  }

  @Override
  public void onHandlePullP2PHistory(String json, boolean isSuccess) {

  }

  @Override
  public void onHandlePullP2THistory(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQueryUnlimitedGroupMembers(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQueryUnlimitedGroups(String json, boolean isSuccess) {

  }

  @Override
  public void onHandleQueryUnlimitedGroupOnlineUsers(String json, boolean isSuccess) {

  }

  /**
   * 统一发送事件和数据
   * @param eventName
   * @param data
   */
  public void sendEvent(String eventName, Object data){
    if(emitter == null){
      emitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
    }
    emitter.emit(eventName, data);
  }
}